<html>
<head>
<title>DatabaseAdapter.java</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
.ln { color: rgb(0,0,0); font-weight: normal; font-style: normal; }
.s0 { color: rgb(0,0,128); font-weight: bold; }
.s1 { }
.s2 { color: rgb(128,128,128); font-style: italic; }
.s3 { color: rgb(0,128,0); font-weight: bold; }
.s4 { color: rgb(0,0,255); }
</style>
</head>
<BODY BGCOLOR="#ffffff">
<TABLE CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#C0C0C0" >
<TR><TD><CENTER>
<FONT FACE="Arial, Helvetica" COLOR="#000000">
DatabaseAdapter.java</FONT>
</center></TD></TR></TABLE>
<pre>

<span class="s0">package </span><span class="s1">com.example.lista_zakupow.database.adapters; 
 
</span><span class="s0">import </span><span class="s1">android.content.Context; 
</span><span class="s0">import </span><span class="s1">android.database.SQLException; 
</span><span class="s0">import </span><span class="s1">android.database.sqlite.SQLiteDatabase; 
</span><span class="s0">import </span><span class="s1">android.database.sqlite.SQLiteOpenHelper; 
</span><span class="s0">import </span><span class="s1">com.example.lista_zakupow.database.DBHelper; 
 
</span><span class="s2">/** 
 * Klasa DatabaseAdapter jest wzorcem projektowym Singletone. 
 * Aby pobrać obiekt Klasy DatabaseAdapter trzeba wywoałać 
 * statyczną metodę getIstance(). 
 */</span><span class="s1"> 
 
</span><span class="s0">public class </span><span class="s1">DatabaseAdapter { 
 
    </span><span class="s0">private static final </span><span class="s1">String DATABASE_NAME = </span><span class="s3">&quot;database.db&quot;</span><span class="s1">; 
 
    </span><span class="s0">private static final int </span><span class="s1">DB_VERSION = </span><span class="s4">1</span><span class="s1">; 
 
    </span><span class="s0">private </span><span class="s1">SQLiteDatabase database; 
 
    </span><span class="s0">private </span><span class="s1">SQLiteOpenHelper dbHelper; 
 
    </span><span class="s0">private </span><span class="s1">DatabaseAdapter() { 
    } 
 
    </span><span class="s0">public static </span><span class="s1">DatabaseAdapter getInstance() { 
        </span><span class="s0">return new </span><span class="s1">DatabaseAdapter(); 
    } 
 
    </span><span class="s0">public </span><span class="s1">SQLiteDatabase open(Context context) { 
        </span><span class="s0">this</span><span class="s1">.dbHelper = </span><span class="s0">new </span><span class="s1">DBHelper(context, DATABASE_NAME, DB_VERSION); 
        </span><span class="s0">this</span><span class="s1">.database = getDatabaseBy(dbHelper); 
        </span><span class="s0">return </span><span class="s1">database; 
    } 
 
    </span><span class="s0">public </span><span class="s1">SQLiteDatabase open(DBHelper dbHelper) { 
        </span><span class="s0">this</span><span class="s1">.dbHelper = dbHelper; 
        </span><span class="s0">this</span><span class="s1">.database = getDatabaseBy(dbHelper); 
        </span><span class="s0">return </span><span class="s1">database; 
    } 
 
    </span><span class="s0">private </span><span class="s1">SQLiteDatabase getDatabaseBy(SQLiteOpenHelper dbHelper){ 
        SQLiteDatabase database; 
        </span><span class="s0">try </span><span class="s1">{ 
            database = dbHelper.getWritableDatabase(); 
        } </span><span class="s0">catch </span><span class="s1">(SQLException ex) { 
            database = dbHelper.getReadableDatabase(); 
        } 
        </span><span class="s0">return </span><span class="s1">database; 
    } 
 
    </span><span class="s0">public void </span><span class="s1">close() { 
        dbHelper.close(); 
        database.close(); 
    } 
}</span></pre>
</body>
</html>